package com.feicheng.house.controller;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.feicheng.house.entity.Card;
import com.feicheng.house.exception.SystemException;
import com.feicheng.house.result.ResultInfo;
import com.feicheng.house.service.ICardService;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author feichengTech
 * @since 2021-11-06
 */
@RestController
@RequestMapping("/house/card")
public class CardController {
	
	@Autowired
	ICardService cardService;
	
	/**
	 * 新增会员卡
	 * @param card
	 * @return 是否新增成功
	 */
	@PostMapping("/info")
	public ResultInfo addCard(Card card) {
		//检查会员卡信息完整性
		if (!StringUtils.hasLength(card.getCardName())
				|| card.getCardMoney() == null
				|| card.getPeriodDate() == null) {
			throw new SystemException("保证会员卡信息完整");
		}
		return cardService.addCard(card);
	}
	
	/**
	 * 删除会员卡
	 * @param cardId
	 * @return 是否删除成功
	 */
	@DeleteMapping("/info")
	public ResultInfo deleteCard(Integer cardId) {
		return cardService.deleteCard(cardId);
	}
	
	/**
	 * 更新会员卡
	 * @param card
	 * @return 是否更新成功
	 */
	@PutMapping("/info")
	public ResultInfo updateCard(Card card) {
		//检查会员卡信息完整性
		if (!StringUtils.hasLength(card.getCardName())
				|| card.getCardMoney() == null
				|| card.getPeriodDate() == null
				|| card.getCardId() == null) {
			throw new SystemException("保证会员卡信息完整");
		}
		return cardService.updateCard(card);
	}
	 
	/**
	 * 根据Id查询会员卡
	 * @param cardId
	 * @return 查询结果
	 */
	@GetMapping("/info")
	public ResultInfo getOneCard(Integer cardId) {
		return cardService.getOneCard(cardId);
	}
	
	/**
	 * 分页查询会员卡
	 * @param currentPage
	 * @param pageSize
	 * @return 分页查询结果
	 */
	@GetMapping("/page")
	public ResultInfo getListCard(
			@RequestParam(defaultValue = "1")Integer currentPage,
			@RequestParam(defaultValue = "10")Integer pageSize) {
		return cardService.getListCard(currentPage, pageSize);
	}
}

